package callback.syn;

/**
 * @author
 * 同步回调，需要一个接口，两个类，
 * 一个类要实现接口声明的方法，
 * 另一个类需要声明一个接口声明的变量，
 *
 */
public class SyncCallback {

}

interface OnGeekEventListener {

    /**
     * this can be any type of method
     */
    void onGeekEvent();
}

class A implements OnGeekEventListener {

    @Override
    public void onGeekEvent()
    {
        System.out.println("Performing main.java.callback after synchronous Task");
    }
}

class B {

    /**
     * listener field
     */
    private OnGeekEventListener mListener;

    /**
     * setting the listener
     */
    public void registerOnGeekEventListener(OnGeekEventListener mListener)
    {
        this.mListener = mListener;
    }

    /**
     * my synchronous task
     */
    public void doGeekStuff()
    {

        // perform any operation
        System.out.println("Performing main.java.callback before synchronous Task");

        // check if listener is registered.
        if (this.mListener != null) {

            // invoke the main.java.callback method of class A
            mListener.onGeekEvent();
        }
    }

    // Driver Function
    public static void main(String[] args)
    {
        B obj = new B();
        OnGeekEventListener mListener = new A();
        obj.registerOnGeekEventListener(mListener);
        obj.doGeekStuff();
    }
}

